package com.compty.paint.web.front;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.compty.core.framework.util.DateUtil;
import com.compty.core.framework.util.StringUtil;
import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Exhibition;
import com.compty.paint.domain.Painting;
import com.compty.paint.manager.ExhibitionManager;

/**
 * @Todo:展览
 * @CreateTime:2011-6-3 下午01:00:40
 */
public class ExhibitionAction extends BaseAction<Exhibition> {

	private static final long serialVersionUID = -7955843044493075042L;

	private ExhibitionManager exhibitionManager;

	// ====================================================
	private List<Exhibition> exhibitionList; //网上展览列表
	private Exhibition exhibition; //特别推荐展览
	private List<Painting> paintingList;
	private Long id;

	/**
	 * @return 列出展览，大图展览，及相关展览列表
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String list() throws Exception {

		Page p = exhibitionManager.findByPage("1", curPage, pageSize);
		
		this.exhibitionList = (List<Exhibition>) p.getResult();
		
		if(exhibitionList!= null  && exhibitionList.size() > 0 ){
			this.exhibition = exhibitionList.get(0);
		}
		
		this.page = exhibitionManager.findAllExhibitionList(curPage,12);
		
		return "list";
	}
	
	/**
	 *@Todo:按省份列出展览
	 *@CreateTime:2011-8-25 下午03:05:05
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listByProvince(){
		
		String pIdStr = request.getParameter("pIdStr");
		if(pIdStr != null && !"".equals(pIdStr)){
			Page page = exhibitionManager.findByPage("1", curPage, pageSize);
			
			this.exhibitionList = (List<Exhibition>) page.getResult();
			
			/************************* 生成海报图片文件到磁盘 begin *****************/
			if(exhibitionList!= null  && exhibitionList.size() > 0 ){
				this.exhibition = exhibitionList.get(0);
			}
			/************************* 生成海报图片文件到磁盘 end *****************/
			if("all".equals(pIdStr)){
				this.page = exhibitionManager.findAllExhibitionList(curPage,12);
			}else{
				if( !StringUtil.isDigit(pIdStr) ){
					return ERROR;
				}
				Long pId = Long.parseLong(pIdStr);
				this.page = exhibitionManager.findExhibitionByProvince(curPage,12,pId);
			}
		}
		
		return "listByProvince";
	}
	
	/**
	 *@Todo:列出全国展览
	 *@CreateTime:2011-8-18 上午11:14:00
	 * @return
	 */
	public String listGlobalExhibition(){

		this.page = exhibitionManager.findByPage("2", curPage, pageSize);
		
		return SUCCESS;
	}
	
	/**
	 * 由ID查找展览信息并展现展览详情
	 *@CreateTime:2011-6-16 下午03:16:57
	 */
	@SuppressWarnings("unchecked")
	public String showDetail(){
		
		Criteria criteria;

		if (sort != null) {
			boolean isAsc = dir.equalsIgnoreCase("asc");
			criteria = exhibitionManager.createCriteria(sort, isAsc);
		} else {
			criteria = exhibitionManager.createCriteria();
		}
		
		criteria.addOrder(Order.desc("id"));
		Page page = exhibitionManager.pagedQuery(criteria, curPage,pageSize);
		
		this.exhibitionList = (List<Exhibition>) page.getResult();
		this.exhibition = exhibitionManager.get(id);
		
		return SUCCESS;
	}
	
	/**
	 *@Todo:由ID查找
	 *@CreateTime:2011-8-18 下午02:22:22
	 * @return
	 */
	public String findById(){
		
		this.exhibition = exhibitionManager.get(id);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", exhibition.getId());
		map.put("title", exhibition.getTitle());
		map.put("artists", exhibition.getArtists());
		map.put("totalCount", exhibition.getTotalCount());
		map.put("unit", exhibition.getUnit());
		map.put("description", exhibition.getDescription());
		map.put("publisher", exhibition.getPublisher());
		map.put("beginTime", DateUtil.toString(exhibition.getBeginTime(), "yyyy年MM月dd日"));
		map.put("endTime", DateUtil.toString(exhibition.getEndTime(),"yyyy年MM月dd日"));
		map.put("imageid", exhibition.getImageInfo().getId());
		map.put("suffix", exhibition.getImageInfo().getSuffix());
		
		String  ajaxData = JSONObject.fromObject(map).toString();
		request.setAttribute("ajaxData", ajaxData);
		
		return "findById";
	}
	
	/**
	 * 展览详情
	 *@CreateTime:2011-6-22 下午05:47:29
	 */
	public String exhibitionDetail(){
		
		this.exhibition = exhibitionManager.get(id);
		return "showDetail";
	}

	public void setExhibitionManager(ExhibitionManager exhibitionManager) {
		this.exhibitionManager = exhibitionManager;
	}

	public List<Exhibition> getExhibitionList() {
		return exhibitionList;
	}

	public void setExhibitionList(List<Exhibition> exhibitionList) {
		this.exhibitionList = exhibitionList;
	}

	public Exhibition getExhibition() {
		return exhibition;
	}

	public void setExhibition(Exhibition exhibition) {
		this.exhibition = exhibition;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<Painting> getPaintingList() {
		return paintingList;
	}

	public void setPaintingList(List<Painting> paintingList) {
		this.paintingList = paintingList;
	}
}
